package com.heima.wemedia.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.model.admin.dtos.ChannelDto;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.wemedia.pojos.WmChannel;
import com.heima.wemedia.mapper.WmChannelMapper;
import com.heima.wemedia.service.WmChannelService;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Description TODO
 * @Author bo.li
 * @Date 2023/3/16 10:51
 * @Version 1.0
 */
@Service
public class WmChannelServiceImpl extends ServiceImpl<WmChannelMapper, WmChannel> implements WmChannelService {

    @Override
    public ResponseResult findAll() {
        return ResponseResult.okResult(list());
    }

    @Override
    public PageResponseResult searchChannels(ChannelDto channelDto) {
        //创建分页请求
        Page<WmChannel> channelPage = new Page<>(channelDto.getPage(),channelDto.getSize());

        //构造查询条件
        QueryWrapper<WmChannel> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("name",channelDto.getName())
                .orderByDesc("created_time");
        List<WmChannel> records = page(channelPage,queryWrapper).getRecords();
        PageResponseResult result = new PageResponseResult();
        result.setData(records);
        result.setTotal((int) channelPage.getTotal());
        result.setSize(channelDto.getSize());
        result.setCurrentPage(channelDto.getPage());
        return result;
    }
}